
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, 2012 and Azure
-- --------------------------------------------------
-- Date Created: 05/11/2014 10:10:13
-- Generated from EDMX file: C:\Users\Emmanuel\Desktop\MyWorkBlog\MyWorkBlogDAL\MyWorkModel.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [MyWorkBlog];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_BlogBlogPost]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BlogPosts] DROP CONSTRAINT [FK_BlogBlogPost];
GO
IF OBJECT_ID(N'[dbo].[FK_BlogPostComment]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Comments] DROP CONSTRAINT [FK_BlogPostComment];
GO
IF OBJECT_ID(N'[dbo].[FK_BlogReply]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Replies] DROP CONSTRAINT [FK_BlogReply];
GO
IF OBJECT_ID(N'[dbo].[FK_BlogrollBlog]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Blogrolls] DROP CONSTRAINT [FK_BlogrollBlog];
GO
IF OBJECT_ID(N'[dbo].[FK_CommentReply]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Replies] DROP CONSTRAINT [FK_CommentReply];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[BlogPosts]', 'U') IS NOT NULL
    DROP TABLE [dbo].[BlogPosts];
GO
IF OBJECT_ID(N'[dbo].[Blogrolls]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Blogrolls];
GO
IF OBJECT_ID(N'[dbo].[Blogs]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Blogs];
GO
IF OBJECT_ID(N'[dbo].[Comments]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Comments];
GO
IF OBJECT_ID(N'[dbo].[ErrorTables]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ErrorTables];
GO
IF OBJECT_ID(N'[dbo].[Replies]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Replies];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Blogs'
CREATE TABLE [dbo].[Blogs] (
    [BlogId] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NOT NULL,
    [Username] nvarchar(max)  NOT NULL,
    [Password] nvarchar(max)  NOT NULL,
    [Country] nvarchar(max)  NOT NULL,
    [FirstName] nvarchar(max)  NOT NULL,
    [LastName] nvarchar(max)  NOT NULL,
    [DateOfBirth] datetime  NOT NULL
);
GO

-- Creating table 'BlogPosts'
CREATE TABLE [dbo].[BlogPosts] (
    [BlogPostId] int IDENTITY(1,1) NOT NULL,
    [PostContent] nvarchar(max)  NOT NULL,
    [PostDate] datetime  NOT NULL,
    [BlogId] int  NOT NULL,
    [LastModifyDate] datetime  NULL,
    [IsDelete] bit  NOT NULL,
    [PostTitle] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'Comments'
CREATE TABLE [dbo].[Comments] (
    [CommentId] int IDENTITY(1,1) NOT NULL,
    [CommentDate] datetime  NOT NULL,
    [BlogPostId] int  NOT NULL,
    [NameOfCommetor] nvarchar(max)  NULL,
    [Email] nvarchar(max)  NULL,
    [CommentContent] nvarchar(max)  NOT NULL,
    [IsDeleted] bit  NOT NULL
);
GO

-- Creating table 'Replies'
CREATE TABLE [dbo].[Replies] (
    [ReplyId] int IDENTITY(1,1) NOT NULL,
    [ReplyDate] datetime  NOT NULL,
    [CommentId] int  NOT NULL,
    [BlogId] int  NOT NULL
);
GO

-- Creating table 'ErrorTables'
CREATE TABLE [dbo].[ErrorTables] (
    [ErrorId] int IDENTITY(1,1) NOT NULL,
    [ErrorMessage] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'Blogrolls'
CREATE TABLE [dbo].[Blogrolls] (
    [BlogrollId] int IDENTITY(1,1) NOT NULL,
    [Link] nvarchar(max)  NULL,
    [Blog_BlogId] int  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [BlogId] in table 'Blogs'
ALTER TABLE [dbo].[Blogs]
ADD CONSTRAINT [PK_Blogs]
    PRIMARY KEY CLUSTERED ([BlogId] ASC);
GO

-- Creating primary key on [BlogPostId] in table 'BlogPosts'
ALTER TABLE [dbo].[BlogPosts]
ADD CONSTRAINT [PK_BlogPosts]
    PRIMARY KEY CLUSTERED ([BlogPostId] ASC);
GO

-- Creating primary key on [CommentId] in table 'Comments'
ALTER TABLE [dbo].[Comments]
ADD CONSTRAINT [PK_Comments]
    PRIMARY KEY CLUSTERED ([CommentId] ASC);
GO

-- Creating primary key on [ReplyId] in table 'Replies'
ALTER TABLE [dbo].[Replies]
ADD CONSTRAINT [PK_Replies]
    PRIMARY KEY CLUSTERED ([ReplyId] ASC);
GO

-- Creating primary key on [ErrorId] in table 'ErrorTables'
ALTER TABLE [dbo].[ErrorTables]
ADD CONSTRAINT [PK_ErrorTables]
    PRIMARY KEY CLUSTERED ([ErrorId] ASC);
GO

-- Creating primary key on [BlogrollId] in table 'Blogrolls'
ALTER TABLE [dbo].[Blogrolls]
ADD CONSTRAINT [PK_Blogrolls]
    PRIMARY KEY CLUSTERED ([BlogrollId] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [BlogId] in table 'BlogPosts'
ALTER TABLE [dbo].[BlogPosts]
ADD CONSTRAINT [FK_BlogBlogPost]
    FOREIGN KEY ([BlogId])
    REFERENCES [dbo].[Blogs]
        ([BlogId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_BlogBlogPost'
CREATE INDEX [IX_FK_BlogBlogPost]
ON [dbo].[BlogPosts]
    ([BlogId]);
GO

-- Creating foreign key on [BlogPostId] in table 'Comments'
ALTER TABLE [dbo].[Comments]
ADD CONSTRAINT [FK_BlogPostComment]
    FOREIGN KEY ([BlogPostId])
    REFERENCES [dbo].[BlogPosts]
        ([BlogPostId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_BlogPostComment'
CREATE INDEX [IX_FK_BlogPostComment]
ON [dbo].[Comments]
    ([BlogPostId]);
GO

-- Creating foreign key on [CommentId] in table 'Replies'
ALTER TABLE [dbo].[Replies]
ADD CONSTRAINT [FK_CommentReply]
    FOREIGN KEY ([CommentId])
    REFERENCES [dbo].[Comments]
        ([CommentId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CommentReply'
CREATE INDEX [IX_FK_CommentReply]
ON [dbo].[Replies]
    ([CommentId]);
GO

-- Creating foreign key on [BlogId] in table 'Replies'
ALTER TABLE [dbo].[Replies]
ADD CONSTRAINT [FK_BlogReply]
    FOREIGN KEY ([BlogId])
    REFERENCES [dbo].[Blogs]
        ([BlogId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_BlogReply'
CREATE INDEX [IX_FK_BlogReply]
ON [dbo].[Replies]
    ([BlogId]);
GO

-- Creating foreign key on [Blog_BlogId] in table 'Blogrolls'
ALTER TABLE [dbo].[Blogrolls]
ADD CONSTRAINT [FK_BlogrollBlog]
    FOREIGN KEY ([Blog_BlogId])
    REFERENCES [dbo].[Blogs]
        ([BlogId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_BlogrollBlog'
CREATE INDEX [IX_FK_BlogrollBlog]
ON [dbo].[Blogrolls]
    ([Blog_BlogId]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------